package com.sykj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bean.bis.*;
import com.sykj.bean.card.BisNurseProjectDto;
import com.sykj.mapper.GameLoadMapper;
import com.sykj.service.*;
import com.sykj.util.IDUtils;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.*;

/**
 * @program: BeautyClub
 * @description:
 * @author: qiaoli
 * @create: 2020-08-10 19:00
 */
@RestController
@RequestMapping("/bisnurseproject")
public class BisNurseProjectController {
     @Autowired
    BisBagRecordService bisBagRecordService;

    @Autowired
    BisBusinessOrderService bisBusinessOrderService;

      @Autowired
    BisProjectRecordService bisProjectRecordService;
    @Autowired
    GameLoadMapper gameLoadMapper;
    @Autowired
    BisNurseProjectService bisNurseProjectService;

    @GetMapping("/queryList/{bagId}")
    public ResultBody queryList(@PathVariable(value = "bagId")String bagId){
        List<BisNurseProjectDto> bisNurseProjectDtos = gameLoadMapper.queryProjectByid(bagId);
      return new ResultBody(CommonEnum.SUCCESS, bisNurseProjectDtos);
    }

    @RequestMapping("/queryProject/{bagId}/{recordId}")
    public ResultBody  queryProjectByidor(@PathVariable("bagId")String bagId,@PathVariable("recordId")String recordId){

        List<BisNurseProjectDto> bisNurseProjectDtos = gameLoadMapper.queryProjectBybagidor(bagId, recordId);
        return  new ResultBody(CommonEnum.SUCCESS, bisNurseProjectDtos);
    }

/**    预约  礼包中的项目
 * 功能描述:  预约 项目
 * @Param: []所需参数 ： 预约的礼包id; 预约的护理项目id, 购买礼包id; 预约的店铺id
 *
 * @Return: com.sykj.util.reponse.ResultBody
 * @Author: Feiwo Sicun
 * @Date: 2020/8/11 23:04
 */
      @Transactional
      @RequestMapping("/booked/{bagId}/{projectId}/{recordId}/{shopId}")
    public ResultBody Booked(@PathVariable("bagId")String bagId,@PathVariable("projectId")String projectId,
                             @PathVariable("recordId")String recordId,@PathVariable("shopId")String shopId
                             ){

          //更据订单id 查询 订单的用户id;
          BisBagRecord byId = bisBagRecordService.getById(recordId);
          Timestamp timeStamp = new Timestamp(new Date().getTime());


          //  添加 营业订单表
          BisBusinessOrder bisBusinessOrder =new BisBusinessOrder();
          bisBusinessOrder.setOrderId(IDUtils.createID());
          bisBusinessOrder.setOrderContent("预约礼包中项目");
          bisBusinessOrder.setProjectId(projectId);
          bisBusinessOrder.setCustomUserId(byId.getCustomUserId());
          //  预约时间  将当期时间延期一天为预约时间
          // 获取当前时间
          Timestamp timestamp0  = new Timestamp(System.currentTimeMillis());
          Calendar c = Calendar.getInstance();
          c.setTime(timestamp0);
          c.add(Calendar.DATE, 1);
          Timestamp time1 = new Timestamp(c.getTimeInMillis());
          // 将延期一天后的时间添加至预约时间中
          bisBusinessOrder.setSubscribeTime(time1);
          bisBusinessOrder.setOrderType("3");
          bisBusinessOrder.setShopId(shopId);
          // 查询 项目的价格
          BisNurseProject byId1 = bisNurseProjectService.getById(projectId);

          bisBusinessOrder.setOrderMoney(byId1.getActivePrice());
//          bisBusinessOrder.setPayMoney(0.01);
          bisBusinessOrder.setPayWay("4");
          bisBusinessOrder.setBuyNum(1);
          bisBusinessOrder.setPayTime(timeStamp);
          bisBusinessOrder.setOrderStatus("0");
          bisBusinessOrder.setCreateTime(timeStamp);
          bisBusinessOrder.setOrderSource("1");

          // 添加服务员工
          bisBusinessOrder.setServerUserId("1595638059502");
          // 操作员工
          bisBusinessOrder.setHandleUserId("1595638059502");
          boolean save1 = bisBusinessOrderService.save(bisBusinessOrder);

          // 将信息添加到 礼包项目预约表 信息中projectId
          BisProjectRecord bisProjectRecord=new BisProjectRecord();
          bisProjectRecord.setRecordId(IDUtils.createID());
          bisProjectRecord.setProjectId(projectId);
          bisProjectRecord.setBagRecordId(recordId);
          bisProjectRecord.setShopId(shopId);
          bisProjectRecord.setProjectStatus("1");

          bisProjectRecord.setUseTime(timeStamp);
          bisProjectRecord.setDrawTime(timeStamp);
          bisProjectRecord.setBagId(bagId);
          bisProjectRecord.setOrders(bisBusinessOrder.getOrderId());




          boolean save = bisProjectRecordService.save(bisProjectRecord);
          if(save==true && save1==true){
              return  new ResultBody(CommonEnum.BOOKED_PROJECT_SUCCESS);
          }

          return new ResultBody(CommonEnum.BOOKED_PROJECT_ERROR);
    }

        @RequestMapping("/querybookedp/{customUserId}")
    public  ResultBody   QueryBookedProjectBy(@PathVariable("customUserId")String customUserId){
            List<BisNurseProjectDto> bisNurseProjectDtos = gameLoadMapper.queryProjectByuser(customUserId);
            return  new ResultBody(CommonEnum.SUCCESS,bisNurseProjectDtos);
    }
    /**
     * 功能描述: 更据id查询 项目名称
     * @Param: [projectId]
     * @Return: com.sykj.util.reponse.ResultBody
     * @Author: Feiwo Sicun
     * @Date: 2020/8/12 14:30
     */
@RequestMapping("/querynameby/{projectId}")
   public ResultBody QueryNameByid(@PathVariable("projectId")String projectId){
    BisNurseProject byId = bisNurseProjectService.getById(projectId);
    return  new ResultBody(CommonEnum.SUCCESS,byId);
   }
@RequestMapping("/querystatus/{customUserId}/{status}")
 public  ResultBody  qureyProjectList(@PathVariable("customUserId")String customUserId,@PathVariable("status")String status){
    List<BisNurseProject> bisNurseProjects = gameLoadMapper.queryBagforStatus(customUserId, status);
    return new ResultBody(CommonEnum.SUCCESS,bisNurseProjects);

}
/**
 * 功能描述:  查询没有消费的项目
 * @Param: [customUserId]
 * @Return: com.sykj.util.reponse.ResultBody
 * @Author: Feiwo Sicun
 * @Date: 2020/8/12 18:28
 */
@RequestMapping("/queryprojectbywei/{customUserId}")
public ResultBody queryProjectBywei(@PathVariable("customUserId")String customUserId){
    List<BisNurseProject> bisNurseProjects = gameLoadMapper.queryBagforidwei(customUserId);
    return new ResultBody(CommonEnum.SUCCESS,bisNurseProjects);
}
/**
 * 功能描述:  查询订单信息
 * @Param: [customUserId]
 * @Return: com.sykj.util.reponse.ResultBody
 * @Author: Feiwo Sicun
 * @Date: 2020/8/12 18:37
 */
@RequestMapping("/queryorderby/{customUserId}/{curpage}/{limit}")
public ResultBody  queryOrderByuser(@PathVariable("customUserId")String customUserId,@PathVariable int curpage, @PathVariable int limit){
    if (curpage > 0 && limit > 0){
        PageHelper.startPage(curpage,limit);
    }
    QueryWrapper queryWrapper=new QueryWrapper();
    queryWrapper.eq("custom_user_id",customUserId);
    List<BisBusinessOrder> list = bisBusinessOrderService.list(queryWrapper);
    PageInfo<BisNurseBag> pageInfo=new PageInfo(list);
    return new ResultBody(CommonEnum.SUCCESS,pageInfo);
}

@RequestMapping("/queryzhan/{curpage}/{limit}")
public  ResultBody  queryByZhan(@PathVariable int curpage, @PathVariable int limit){
    if (curpage > 0 && limit > 0){
        PageHelper.startPage(curpage,limit);
    }
    List<BisNurseProject> bisNurseProjects = gameLoadMapper.queryDianzhanforProject();
    PageInfo<BisNurseBag> pageInfo=new PageInfo(bisNurseProjects);
    return new ResultBody(CommonEnum.SUCCESS,pageInfo);
}
/**
 * 功能描述:  根据名字 模糊查询
 * @Param: [projectName, curpage, limit]
 * @Return: com.sykj.util.reponse.ResultBody
 * @Author: Feiwo Sicun
 * @Date: 2020/8/14 16:22
 */
@RequestMapping("/querylikeName")
public ResultBody queryLikeName(String projectName, int curpage,  int limit){
    if (curpage > 0 && limit > 0){
        PageHelper.startPage(curpage,limit);
    }
    List<BisNurseProject> bisNurseProjects = gameLoadMapper.queryNameByproject(projectName);
    PageInfo<BisNurseBag> pageInfo=new PageInfo(bisNurseProjects);
    return new ResultBody(CommonEnum.SUCCESS,pageInfo);
}
}
